const Router = require('koa-router');
const OtherRouter = new Router();
const R = require('../utils/R');
const fs = require("fs")
const path = require("path");

OtherRouter.prefix('/api/v1/other');

OtherRouter.post('/upload', async (ctx, next) => {
    // console.log(ctx.request)
    // console.log(ctx.request.files.file, '==========================')
    let file = ctx.request.files.file;
    const reader = fs.createReadStream(file.path);
    let newFileName = new Date().getTime() + file.name;
    // console.log(newFileName)
    let filePath = path.join(__dirname, '../public/images/') + `/${newFileName}`;
    // console.log(filePath + "=============")
    // 创建可写流
    const upStream = fs.createWriteStream(filePath);
   
    // 可读流通过管道写入可写流
    reader.pipe(upStream);

    ctx.body = {
        code: 200,
        url: `http://139.199.69.126:10000/images/${newFileName}`
    }
})


module.exports = OtherRouter